package com.njtoyo.taxi.admin.logic.geo;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.mapper.master.entity.GeoDriverRegion;
import com.njtoyo.taxi.admin.mapper.master.entity.GeoDriverRegionArea;
import com.njtoyo.taxi.admin.mapper.master.mapper.GeoDriverRegionAreaMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.GeoDriverRegionMapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.RegionAreaCreateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DriverRegionAreaLogic {
    @Autowired
    private GeoDriverRegionAreaMapper geoDriverRegionAreaMapper;
    @Autowired
    private GeoDriverRegionMapper geoDriverRegionMapper;

    public void createInsert(Long regionId, List<Long> areaIds) {
        List<RegionAreaCreateWrapper> list = new ArrayList<>();
        for (Long areaId : areaIds) {
            RegionAreaCreateWrapper item = new RegionAreaCreateWrapper();
            item.setRegionId(regionId);
            item.setAreaId(areaId);
            list.add(item);
        }

        geoDriverRegionAreaMapper.createBatch(list);
    }

    public GeoDriverRegion getRegionByAreaId(Long areaId) {
        GeoDriverRegionArea geoDriverRegionArea = geoDriverRegionAreaMapper.selectOne(Wrappers.<GeoDriverRegionArea>lambdaQuery()
                .eq(GeoDriverRegionArea::getAreaId, areaId));
        if (ObjectUtil.isNull(geoDriverRegionArea)) {
            return null;
        }
        return geoDriverRegionMapper.selectById(geoDriverRegionArea.getDriverRegionId());

    }
}
